import { Doctor } from '../../types/doctor'
import  { execute,select }  from '../../utils/db'
import { Page, PageReq } from '../../types/page'

export class DoctorDao {
  /**
   * 创建医生
   */
  static async create(doctor: Omit<Doctor, 'id'>): Promise<number | undefined> {
    const result = await execute(
      'INSERT INTO doctor (name, tel, account, password) VALUES (?, ?, ?, ?) RETURNING id',
      [doctor.name, doctor.tel, doctor.account, doctor.password]
    )
    return result?.lastInsertId;
  }


  /**
   * 更新医生信息
   */
  static async update(doctor: Partial<Doctor>): Promise<void> {
    const fields = []
    const values = []

    if (doctor.name) {
      fields.push('name = ?')
      values.push(doctor.name)
    }
    if (doctor.tel) {
      fields.push('tel = ?')
      values.push(doctor.tel)
    }
    if (doctor.account) {
      fields.push('account = ?')
      values.push(doctor.account)
    }
    if (doctor.password) {
      fields.push('password = ?')
      values.push(doctor.password)
    }

    if (fields.length === 0) return

    await execute(
      `UPDATE doctor SET ${fields.join(', ')} WHERE id = ?`,
      [...values, doctor.id]
    )
  }

  /**
   * 删除医生
   */
  static async delete(id: number): Promise<void> {
    await execute('DELETE FROM doctor WHERE id = ?', [id])
  }

  /**
   * 获取医生详情
   */
  static async getById(id: number): Promise<Doctor> {
    const result = await select<Doctor>(
      'SELECT * FROM doctor WHERE id = ? LIMIT 1',
      [id]
    )
    return result[0];
  }

  /**
   * 获取医生列表
   */
  static async list(): Promise<Doctor[]> {
    const result = await select<Doctor[]>('SELECT * FROM doctor')
    return result;
  }

  /**
   * 分页查询医生列表
   */
  static async page(params: PageReq<Doctor>): Promise<Page<Doctor>> {
    const { pageNum, pageSize } = params
    const offset = (pageNum - 1) * pageSize
    const fields = []
    const values = []
    const doctor = params.params
    if (doctor.name) {
      fields.push('name like ?')
      values.push(`%${doctor.name}%`)
    }
    if (doctor.tel) {
      fields.push('tel like ?')
      values.push(`%${doctor.tel}%`)
    }
    if (doctor.account) {
      fields.push('account like ?')
      values.push(`%${doctor.account}%`)
    }
    const condition = fields.length > 0 ? `WHERE ${fields.join(' AND ')}` : ''
    const countSql = `SELECT COUNT(*) as total FROM doctor ${condition}`
    const listSql = `SELECT * FROM doctor ${condition} LIMIT ${pageSize} OFFSET ${offset}`
    const totalResult = await select<number>(countSql, values)
    const listResult = await select<Doctor[]>(listSql, values)
    const result = {
      total: totalResult[0].total || 0,
      list: listResult
    }
    return result
    // return {
    //   total: (totalResult || 0),
    //   list: (listResult || [])
    // }
  }

  /**
   * 根据账号获取医生
   */
  static async getByAccount(account: string): Promise<Doctor> {
    const result = await select<Doctor>(
      'SELECT id,name,tel,account,password FROM doctor WHERE account = ? LIMIT 1',
      [account]
    )
    return result[0];
  }

    
  /**
   * 密码修改
   */
  static async updatePassword(id: number, password: string) {
    const sql = 'UPDATE doctor SET password = ? WHERE id = ?'
    return execute(sql, [password, id])
  }
}